from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, Field


class UserBase(BaseModel):
    """共用字段"""
    nick_name: Optional[str] = Field(None, alias="nickName", max_length=64, description="用户昵称")
    mobile: Optional[str] = Field(None, alias="mobile", max_length=20, description="手机号码")
    platform: Optional[str] = Field(None, max_length=10, description="平台(h5/pc)")


class MemberVo(UserBase):
    """返回给前端"""
    user_id: int = Field(..., alias="userId", description="用户主键")
    gender: Optional[str] = Field(None, alias="gender", description="性别")
    country: Optional[str] = Field(None, alias="country", description="国家")
    province: Optional[str] = Field(None, alias="province", description="省份")
    city: Optional[str] = Field(None, alias="city", description="城市")
    address_id: Optional[int] = Field(None, alias="addressId", description="默认地址ID")
    balance: Optional[Decimal] = Field(None, alias="balance", description="余额")
    points: Optional[int] = Field(None, alias="points", description="积分")
    pay_money: Optional[Decimal] = Field(None, alias="payMoney", description="累计消费")
    expend_money: Optional[Decimal] = Field(None, alias="expendMoney", description="已支出金额")
    grade_id: Optional[int] = Field(None, alias="gradeId", description="会员等级ID")
    avatar_url: Optional[str] = Field(None, alias="avatarUrl", description="头像完整URL")

    class Config:
        orm_mode = True
        allow_population_by_field_name = True
        json_encoders = {Decimal: lambda v: float(v) if v is not None else None}
